package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocom.common.entity.card.EntityXfPointInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.payconfig.AddTollPointInfoRequest;
import com.ocom.common.request.payconfig.GetTollPointInfoRequest;
import com.ocom.common.request.payconfig.UpdateTollPointInfoRequest;
import com.ocom.common.request.web.DelTollPointRequest;
import com.ocom.common.utils.CommonUtil;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoPointInfo;
import com.ocom.payconfig.interceptor.ScopeQueryWrapper;
import com.ocom.payconfig.service.XfPointInfoService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class XfPointInfoServiceImpl extends ServiceImpl<IDaoPointInfo, EntityXfPointInfo> implements XfPointInfoService {

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB)
    public PageInfo getTollPointInfo(Long companyId,GetTollPointInfoRequest request){
        QueryWrapper<EntityXfPointInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",request.getCom_id());
        if(request.getPoint_id()!=null && request.getPoint_id()>=0){
            queryWrapper.eq("point_id",request.getPoint_id());
        }
        if(!CommonUtil.isNull(request.getPoint_name())){
            queryWrapper.eq("point_name",request.getPoint_name());
        }
        if(request.getParent_point_id()>=0){
            queryWrapper.eq("parent_point_id",request.getParent_point_id());
        }
        if(request.getMerch_id()>=0){
            queryWrapper.eq("merch_id",request.getMerch_id());
        }
        if(request.getPage()!=null && request.getPage()>0){
            Integer size=request.getSize()>0?request.getSize():50;
            PageHelper.startPage(request.getPage(),size);
            List<EntityXfPointInfo> pointInfoPageInfo=this.list(queryWrapper);
            PageInfo pageInfo=new PageInfo<>(pointInfoPageInfo);
            return pageInfo;
        }else{
            if(request.getSize()==null){
                request.setSize(150);
            }
            Integer size=request.getSize()>0?request.getSize():150;
            PageHelper.startPage(1,size);
            List<EntityXfPointInfo> pointInfoPageInfo=this.list(queryWrapper);
            PageInfo pageInfo=new PageInfo<>(pointInfoPageInfo);
            return pageInfo;
        }
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB)
    public EntityXfPointInfo addTollPointInfo(Long companyId,AddTollPointInfoRequest request){
        EntityXfPointInfo entityXfPointInfo= new EntityXfPointInfo();
        entityXfPointInfo.setPointName(request.getPointName());
        entityXfPointInfo.setParentPointId(request.getParentPointId());
        entityXfPointInfo.setComId(request.getComId());
        entityXfPointInfo.setMerchId(request.getMerchId());
        entityXfPointInfo.setRemark(request.getRemark());
        entityXfPointInfo.setCreateUser(request.getCreateUser());
        entityXfPointInfo.setCreateTime(new Date());
        entityXfPointInfo.setPointOrgin(request.getPointOrgin());
        if(request.getParentPointId()>0){
            QueryWrapper<EntityXfPointInfo> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("com_id",request.getComId());
            queryWrapper.eq("point_id",request.getParentPointId());
            queryWrapper.last("limit 1");
            EntityXfPointInfo entityXfPointInfo1 =this.getOne(queryWrapper);
            if(entityXfPointInfo1==null){
                return null;
            }
            entityXfPointInfo.setMerchId(entityXfPointInfo1.getMerchId());
            if(CommonUtil.isNull(entityXfPointInfo1.getPointOrgin())){
                entityXfPointInfo.setPointOrgin(","+request.getParentPointId()+",");
            }else{
                entityXfPointInfo.setPointOrgin(entityXfPointInfo1.getPointOrgin()+request.getParentPointId()+",");
            }
        }


        boolean b=this.save(entityXfPointInfo);
        if(!b){
            return null;
        }
        return entityXfPointInfo;
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB)
    public EntityXfPointInfo updateTollPointInfo(Long companyId,UpdateTollPointInfoRequest request){
        QueryWrapper<EntityXfPointInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",request.getCom_id());
        queryWrapper.eq("point_id",request.getPoint_id());
        queryWrapper.last("limit 1");
        EntityXfPointInfo entityXfPointInfo=this.getOne(queryWrapper);
        if(entityXfPointInfo== null){
            //TODO
        }
        if(!CommonUtil.isNull(request.getPoint_name())){
            entityXfPointInfo.setPointName(request.getPoint_name());
        }
        entityXfPointInfo.setParentPointId(request.getParent_point_id());
        entityXfPointInfo.setMerchId(request.getMerch_id());
        if(!CommonUtil.isNull(request.getRemark())){
            entityXfPointInfo.setRemark(request.getRemark());
        }
        if(!CommonUtil.isNull(request.getUpdate_user())){
            entityXfPointInfo.setUpdateUser(request.getUpdate_user());
        }
        entityXfPointInfo.setUpdateTime(new Date());
        this.updateById(entityXfPointInfo);
        return entityXfPointInfo;
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB)
    public boolean deleteTollPointInfo(Long companyId,DelTollPointRequest request) {
        QueryWrapper<EntityXfPointInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",request.getComId());
        queryWrapper.eq("point_id",request.getPointId());
        queryWrapper.eq("merch_id",request.getMerchId());
        return this.remove(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB)
    public List<EntityXfPointInfo> getTollPointList(Long companyId, List<Integer> ins) {
        QueryWrapper<EntityXfPointInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("com_id", companyId);
//        if (ins == null || ins.size() == 0) {
//            return this.list(queryWrapper);
//        }
//        if (ins.size() == 1) {
//            queryWrapper.eq("merch_id", ins.get(0));
//            return this.list(queryWrapper);
//        }
        queryWrapper.in("point_id", ins);
        return this.list(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityXfPointInfo> selectPointList(Long comId, Integer merchId ,String strMerchs) {
        QueryWrapper<EntityXfPointInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("com_id", comId);
        String[] arr=strMerchs.split(",");
        if (merchId==0){
            queryWrapper.notIn("merch_id", arr);
        }else {
            queryWrapper.in("merch_id", arr);
        }
        return this.list(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityXfPointInfo> selectPointListS(Long comId) {
        QueryWrapper<EntityXfPointInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("com_id", comId);
        return this.list(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    public EntityXfPointInfo getTollPointInfo(Long companyId, Integer pointId) {
        QueryWrapper<EntityXfPointInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("com_id", companyId);
        queryWrapper.eq("point_id", pointId);
        return this.getOne(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityXfPointInfo> getAllChildPoint(Long companyId, Integer pointId) {
        QueryWrapper<EntityXfPointInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("point_id","parent_point_id");
        queryWrapper.eq("com_id", companyId);
        List<EntityXfPointInfo> listOrg =  this.list(queryWrapper);
        if(pointId<=0 || listOrg.size()==0){
            return listOrg;
        }
        if(pointId>0){
            queryWrapper.eq("parent_point_id", pointId);
        }
        List<EntityXfPointInfo> list2 =  this.list(queryWrapper);
        return getAllChild(listOrg,list2,0);
    }


    /**
     *
     * @param org
     * @param list
     * @return
     */
    private List<EntityXfPointInfo>  getAllChild(List<EntityXfPointInfo> org, List<EntityXfPointInfo> list, Integer loop) {
        loop++;
        if(loop>=20){
            return list;
        }
        List<EntityXfPointInfo> nowList = list;
        Integer beforeSize = nowList.size();
        List<Integer> merchIds = nowList.stream().map(EntityXfPointInfo::getPointId).collect(Collectors.toList());
        for (EntityXfPointInfo entityFoodstuffClass :org){
            if(!merchIds.contains(entityFoodstuffClass.getPointId()) && merchIds.contains(entityFoodstuffClass.getParentPointId())){
                nowList.add(entityFoodstuffClass);
            }
        }
        Integer afterSize = nowList.size();
        if(beforeSize.equals(afterSize)){
            return  list;
        }else{
            return  getAllChild(org,nowList,loop);
        }
    }


    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityXfPointInfo> getTollPointList(Long companyId) {
        ScopeQueryWrapper<EntityXfPointInfo> queryWrapper = new ScopeQueryWrapper<>();
        queryWrapper.eq("com_id", companyId);
        return this.list(queryWrapper);
    }

}
